package com.yulang.springbootnetty.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * 实现客户端发送请求服务器会返回hello netty
 */
public class HelloServer {

    public static void main(String[] args) throws Exception {
        //定义一对线程组用于接收客户端连接但是不做任何事情
        //定义主线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        //定义从线程组 工作线程组
        EventLoopGroup workGroup = new NioEventLoopGroup();
        try {
            //netty服务器的创建 serverBootstrap是一个启动类
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap
                    .group(bossGroup, workGroup)
                    .channel(NioServerSocketChannel.class)//设置channel
                    .childHandler(new HelloServerInit())//子处理器用于处理worker
            ;
            //用于启动server
            ChannelFuture future = serverBootstrap.bind(8088).sync();
            //关闭通道
            future.channel().closeFuture().sync();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            workGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }



    }

}
